﻿// //  ********************************************************************************
// //  Copyright (C) 2016 Sebastian Kenter. All rights reserved.
// // 
// //  Name    : MultiServerEngine | ServerFramework | ServerPeerFactory.cs
// //  Created : 2016 - 02 - 16 20:53
// //  Modified: 2016 - 02 - 20 12:33
// //  ********************************************************************************

using ExitGames.Logging;
using Photon.SocketServer;
using ServerFramework.Interfaces.Application;
using ServerFramework.Interfaces.Configuration;
using ServerFramework.Interfaces.Handler;
using ServerFramework.Interfaces.Server;

namespace ServerFramework.Implementation.Server
{
    /// <summary>
    ///     Class ServerPeerFactory.
    /// </summary>
    public class ServerPeerFactory : IServerPeerFactory
    {
        private IServerApplication Server { get; }
        private IServerPeerRegistry<IServerPeer> ServerPeerRegistry { get; }
        private ITelegramHandlerRegistry HandlerRegistry { get; }
        public ILogger Logger { get; }

        public ServerPeerFactory(ILogger logger, IServerApplication server,
                                 IServerPeerRegistry<IServerPeer> serverPeerRegistry,
                                 ITelegramHandlerRegistry handlerRegistry)
        {
            Logger = logger;
            Server = server;
            ServerPeerRegistry = serverPeerRegistry;
            HandlerRegistry = handlerRegistry;
        }

        public T CreatePeer<T>(IPeerConfig peerConfig) where T : class
        {
            Logger.InfoFormat("About to create a new server peer on server={0}", Server.ServerType);
            return
                new ServerPeer(peerConfig.GetPeerConfig<InitResponse>().Protocol,
                    peerConfig.GetPeerConfig<InitResponse>().PhotonPeer, Logger, Server, ServerPeerRegistry,
                    HandlerRegistry) as T;
        }
    }
}
